package com.crrcdt.vue_project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crrcdt.vue_project.entity.Attribute;
import com.crrcdt.vue_project.result.Result;
import com.crrcdt.vue_project.result.ResultFactory;
import com.crrcdt.vue_project.service.AttributeService;
import com.crrcdt.vue_project.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lj on 2020/10/23.
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/private/v1")
@Slf4j
public class AttrController {
    //参数列表
    @Autowired
    private AttributeService attributeService;
    @Autowired
    private CategoryService categoryService;

    //     参数列表
    @GetMapping("categories/{id}/attributes")
    public Result getAttrs(@PathVariable("id") Integer id, @RequestParam("sel") String sel) {
        final List<Attribute> attributes = attributeService.list(new QueryWrapper<Attribute>().eq("cat_id", id).eq("attr_sel", sel));
        if (attributes != null) {
            return ResultFactory.buildSuccessResult(attributes, "获取参数成功！");
        }
        return ResultFactory.buildFailResult("获取参数失败！");
    }

    //    添加动态参数或者静态属性
    @PostMapping("categories/{id}/attributes")
    public Result addAttrs(@PathVariable("id") Integer id,
                           @RequestBody Attribute attribute) {
        attribute.setAttrWrite(attribute.getAttrSel().equals("many") ? "list" : "manual");
        attribute.setCatId(id);
        if(attribute.getAttrVals()==null){
            attribute.setAttrVals("");
        }
        final boolean res = attributeService.save(attribute);
        if (res) {
            return ResultFactory.buildSuccessResult(attribute, "添加参数成功！");
        }
        return ResultFactory.buildFailResult("添加参数失败！");
    }

    //    删除参数
    @DeleteMapping("categories/{id}/attributes/{attrid}")
    public Result removeAttrs(@PathVariable("id") Integer id, @PathVariable("attrid") Integer attrid) {
        final boolean res = attributeService.remove(new QueryWrapper<Attribute>().eq("attr_id", attrid));
        if (res) {
            return ResultFactory.buildSuccessResult(null, "删除参数成功！");
        }
        return ResultFactory.buildFailResult("删除参数失败！");
    }

    //    根据 ID 查询参数
    @GetMapping("categories/{id}/attributes/{attrId}")
    public Result getAttrsById(@PathVariable("id") Integer id, @PathVariable("attrId") Integer attrId) {
        final Attribute attribute = attributeService.getById(attrId);
        if (attribute != null) {
            return ResultFactory.buildSuccessResult(attribute, "获取参数成功！");
        }
        return ResultFactory.buildFailResult("获取参数失败！");
    }

    //     编辑提交参数
    @PutMapping("categories/{id}/attributes/{attrId}")
    public Result updateAttrsById(@PathVariable("id") Integer id, @PathVariable("attrId") Integer attrId,
                                  @RequestBody Attribute attribute) {
        attribute.setAttrId(attrId);
        final boolean res = attributeService.updateById(attribute);
        if (res) {
            final Attribute attributeNew = attributeService.getById(attrId);
            return ResultFactory.buildSuccessResult(attributeNew, "编辑参数成功！");
        }
        return ResultFactory.buildFailResult("编辑参数失败！");
    }
}
